#include <string.h>
#include <stdarg.h>
#include <stdio.h>

#include "CanPrint.h"

#ifdef CAN_PRINT

Ret MyPrintf(const char *format, ...)
{
	int ret = OK;
	int len = 0;
	char PrintBuffer[MAX_LENGTH] = { 0 };
	va_list vlist;
	
	if (NULL == format)
	{
		return INVALID_PARAMETER;
	}

	va_start(vlist, format);
	vsnprintf(PrintBuffer, MAX_LENGTH, format, vlist);
	len = strlen(PrintBuffer);
	if (MAX_LENGTH == len)
	{
		ret = OVERFLOW;
	}

	if (!ret)
	{
		/*add a terminator*/
		len++;

		/*fill*/
		if (len % 8)
		{
			int temp = (len / 8 + 1) * 8;
			while (len < temp)
			{
				PrintBuffer[len++] = 0;
			}
		}
		
		char* temp = PrintBuffer;
		while (len)
		{
			/*send 8 bytes everytimes by CAN*/
			// for (int i = 0; i < 8; i++)
			// {
			// 	printf("%c", *(char*)(temp++));
			// }

			len -= 8;
		}
	}

	va_end(vlist);

	return ret;
}

#endif
